package org.lilred.nzbberry.webservice;

import org.lilred.nzbberry.http.HttpRequestDispatcher;
import org.lilred.nzbberry.http.RequestsHttp;
import org.lilred.nzbberry.models.ConfigModel;

public abstract class SabnzbdClient implements RequestsHttp {
	protected HttpRequestDispatcher dispatcher;
	
	public static String getApiUrl(String mode) {
		StringBuffer apiUrl = new StringBuffer();
		apiUrl.append(ConfigModel.getConfig().getSabnzbdAddress());

		//Set default URL to http:// to avoid illegal argument exception from Connector class
		if (apiUrl.length() == 0) return "http://";
		
		//SABnzbd urls end with a trailing slash
		if (apiUrl.charAt(apiUrl.length() - 1) != '/') apiUrl.append('/');
		
		//Append the API key
		apiUrl.append("api?apikey=");
		apiUrl.append(ConfigModel.getConfig().getSabnzbdApiKey());
		apiUrl.append("&output=json");
		apiUrl.append("&mode=" + mode);

		return apiUrl.toString();
	}

	public void requestFailed(byte[] result, int responseCode) {
		System.out.println("HTTP Request Failed.  Response code: " + responseCode + ". Output: ");
		System.out.println(new String(result));
	}
}
